<?php
/**
 * Created by IntelliJ IDEA.
 * User: jimmyhsu
 * Date: 2017/4/3
 * Time: 18:26
 */
include('facepp.php');
include('../Teacher/checkLogin.php');
$course_id = $_GET['course_id'];
$facepp = new facepp();
//$jsonArr = $facepp->remove_faceset_by_id($course_id, 0);
//print_r($jsonArr);
//usleep(500);
$jsonArr = $facepp->add_faceset($course_id);

sleep(1);
//print_r($jsonArr);
if ($jsonArr && isset($jsonArr['faceset_token'])) {
    $sql = "update course set faceset_token='".$jsonArr['faceset_token']."' where course_id='$course_id'";
    mysqli_query($cn, $sql);
    $sql = "select token from takes natural join userinfo where course_id='$course_id'";
    $rs = mysqli_query($cn, $sql);
    $i = 0;
    $face_tokens = "";
    while ($line = mysqli_fetch_assoc($rs)) {
        if ($line['token'] != '') {
            if ($i < 5) {
                $face_tokens .= $line['token'];
                $face_tokens .= ",";
                $i++;
            } else {
                $face_tokens = substr($face_tokens, 0, strlen($face_tokens) - 1);
                $result = $facepp->add_face($jsonArr['faceset_token'], $face_tokens);
//                print_r($result);
                sleep(1);
                $face_tokens = "";
                $i = 0;
            }
        }
    }
    if ($face_tokens != "") {
        $face_tokens = substr($face_tokens, 0, strlen($face_tokens) - 1);
        $result = $facepp->add_face($jsonArr['faceset_token'], $face_tokens);
//        print_r($result);

    }

    echo "success";
} else {
    echo $jsonArr['error_message'];
}